package com.yixun.qfbao.hander;

import com.yixun.qfbao.apidto.AgentRegisterSwitchReqDto;
import com.yixun.qfbao.apidto.AgentRegisterSwitchResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.Company;
import com.yixun.qfbao.model.dto.CompanyDto;
import com.yixun.qfbao.service.CompanyService;
import com.yixun.qfbao.vo.AgentRegisterSwitchReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @classname: agentRegisterSwitchHander.java
 * @author: Beau
 * @create: 2019/12/11 11:38
 * @version: 1.0.1
 **/
@Service("agentRegisterSwitchHander")
public class AgentRegisterSwitchHander extends CommonAbstractHander {
    @Autowired
    private CompanyService companyService;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        AgentRegisterSwitchReqVo reqDto1 = (AgentRegisterSwitchReqVo) reqDto;
        AgentRegisterSwitchReqDto dto = reqDto1.getParams();
        BaseResponseDTO<AgentRegisterSwitchResDto> res = new BaseResponseDTO<AgentRegisterSwitchResDto>();
        AgentRegisterSwitchResDto resDto = new AgentRegisterSwitchResDto();

        String ifRegisterFlg = dto.getIfRegisterFlg();
        Company company = companyService.selectById(Long.valueOf(dto.getCompanyId()));
        CompanyDto companyDto = new CompanyDto();
        if (!company.getIfOpenSysMsg().equals(ifRegisterFlg)) {
            resDto.setResultMsg("您已更改注册状态,请刷新页面");
            res.setResData(resDto);
            res.setResult(ResultEnum.FAIL.getNo());
            return res;
        }
        if ("0".equals(ifRegisterFlg)) {
            //当前注册状态为禁用,更改注册状态为启用状态
            companyDto.setIfOpenSysMsg("1");
        } else {
            //当前注册状态为启用,更改注册状态为禁用状态
            companyDto.setIfOpenSysMsg("0");
        }
        companyDto.setId(company.getId());
        int lows = companyService.update(companyDto);
        if (lows > 0) {
            resDto.setResult(ResultEnum.SUCCESS.getNo());
            res.setResData(resDto);
            res.setResult(ResultEnum.SUCCESS.getNo());
            return res;
        }
        resDto.setResult(ResultEnum.FAIL.getNo());
        res.setResult(ResultEnum.FAIL.getNo());
        res.setResData(resDto);
        res.setErrorMsg("更新状态失败,请刷新页面重试");
        return res;
    }
}
